package ru.spb.osll.myfit.client.widget;

import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.Event;

public class Tooltip extends PopupPanel {
	
    //tooltip class name, defined in your css file
    public static final String DEF_TOOLTIP_STYLE = "tooltip";
    public static final String DEF_TOOLTIP_WIDTH = "300px";
    public static final String DEF_TOOLTIP_HEIGHT = "";
    //the distance from mouse to the tooltip
    public static final int DEF_MOUSE_OFFSET_X = 20;
    public static final int DEF_MOUSE_OFFSET_Y = 20;
    
    private HTML container;
    private int offsetX;
    private int offsetY;
    
    public Tooltip(String content) {
        super();
        init(content);
    }
    
    public Tooltip(String content, String style){
        super();
        init(content);
	setStyle(style);
    }
    
    public Tooltip(String content, String width, String height){
        super();
        init(content);
	setTooltipSize(width, height);
	setStyle(null);
    }
    
    public Tooltip(String content, String width, String height, String style){
        super();
        init(content);
	setTooltipSize(width, height);
	setStyle(style);
    }
    
    //initialization
    private void init(String content){
        this.container = new HTML(content);
        setWidget(container);
        
        this.offsetX = DEF_MOUSE_OFFSET_X;
        this.offsetY = DEF_MOUSE_OFFSET_Y;
    }

    private void setTooltipSize(String width, String height) {
        if(width == null){
            this.setWidth(DEF_TOOLTIP_WIDTH);
        }else{
            this.setWidth(width);
        }
        
        if(height == null){
            this.setHeight(DEF_TOOLTIP_HEIGHT);
        }else{
            this.setHeight(height);
        }
    }

    private void setStyle(String style) {
        if(style == null || style.length() == 0){
            style = DEF_TOOLTIP_STYLE;
        }
        setStyleName(style);
    }
    
    public HTML getContainer(){
        return container;
    }
    
    //don't process any event, let parent process it
    public boolean onEventPreview(Event event) {
       return true;  
    }
    
    public int getOffsetX() {
        return offsetX;
    }
    
    public void setOffsetX(int offsetX) {
        this.offsetX = offsetX;
    }
    
    public int getOffsetY() {
        return offsetY;
    }
    
    public void setOffsetY(int offsetY) {
        this.offsetY = offsetY;
    }
}
